package com.bndg.blackjack.ui

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.bndg.blackjack.AppUtils
import com.bndg.blackjack.R
import com.bndg.blackjack.widget.CircleTransitionView
import com.bndg.blackjack.widget.VectorTextView


/**
 * @author r
 * @date 2024/12/9
 * @description 菜单界面
 */
class WelcomeActivity : AppCompatActivity() {
    private var currentTimeMillis: Long = 0
    private lateinit var tvOnline: CircleTransitionView
    private lateinit var tvDealer: CircleTransitionView
    private lateinit var tvPlayer: CircleTransitionView
    private lateinit var tv_vt: VectorTextView
    private lateinit var tvOffline: View
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        AppUtils.application = application
        tvOnline = findViewById(R.id.tv_online)
        tvOffline = findViewById(R.id.tv_offline)
        tvDealer = findViewById(R.id.tv_dealer)
        tvPlayer = findViewById(R.id.tv_player)
        tvDealer.setOnClickListener {
            BlackjackActivity.start(this, true)
        }
        tvPlayer.setOnClickListener {
            showIpPortDialog()
        }
        tvOnline.setOnClickListener {
            startScaleAnimation()
        }
        tvOffline.setOnClickListener {
            BlackjackActivity.start(this, false)
        }
        tv_vt = findViewById(R.id.tv_vt)
        tv_vt.setOnClickListener {
            tv_vt.isChecked = !tv_vt.isChecked
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showIpPortDialog() {
        // 创建一个 LinearLayout，水平排列
        val linearLayout = LinearLayout(this)
        linearLayout.orientation = LinearLayout.HORIZONTAL

        // 创建两个 EditText
        val etIp = EditText(this)
        val etPort = EditText(this)
        etIp.hint = "192.168.1.4"
        etPort.hint = "8080"
        etPort.setText("8080")
        etIp.setText(AppUtils.get("ipAddress"))
        // 设置 EditText 的布局参数
        val layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2f)
        etIp.layoutParams = layoutParams
        val layoutParams2 = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
        etPort.layoutParams = layoutParams2

        // 将 EditText 添加到 LinearLayout 中
        linearLayout.addView(etIp)
        linearLayout.addView(etPort)

        // 创建 AlertDialog
        val dialog = AlertDialog.Builder(this)
            .setTitle("提示")
            .setMessage("请输入IP地址和端口号：")
            .setView(linearLayout) // 设置视图为包含两个输入框的 LinearLayout
            .setPositiveButton("确定") { dialogInterface, _ ->
                // 获取两个输入框的内容
                val ip = etIp.text.toString()
                val port = etPort.text.toString()
                dialogInterface.dismiss()
                if (ip.isNotEmpty() && port.isNotEmpty()) {
                    AppUtils.put("ipAddress", ip)
                    BlackjackActivity.start(this, true, true, ip, port.toInt())
                } else {
                    Toast.makeText(this, "请输入正确的IP地址和端口号", Toast.LENGTH_SHORT).show()
                }
            }
            .setNegativeButton("取消") { dialogInterface, _ ->
                dialogInterface.dismiss()
            }
            .create()

        dialog.show()
    }

    private fun startScaleAnimation(isReverse: Boolean = false) {
        val start = if (isReverse) 0.9f else 1f
        val end = if (isReverse) 1f else 0.9f
        val scaleXAnimator = ObjectAnimator.ofFloat(tvOnline, "scaleX", start, end)
        val scaleYAnimator = ObjectAnimator.ofFloat(tvOnline, "scaleY", start, end)
        scaleXAnimator.duration = 300
        scaleYAnimator.duration = 300
        val animatorSet = AnimatorSet()
        animatorSet.playTogether(scaleXAnimator, scaleYAnimator) // 同时执行平移和旋转
        scaleXAnimator.addUpdateListener {
            val progress = it.animatedFraction
            var calcRadius = (tvOnline.width.coerceAtMost(tvOnline.height) / 2f) * progress
            if (isReverse) {
                calcRadius = (tvOnline.width.coerceAtMost(tvOnline.height) / 2f) - calcRadius
            }
            val targetRadius = calcRadius
            tvOnline.setRadius(targetRadius)
        }
        scaleXAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {
                if (!isReverse) {
                    tvDealer.visibility = View.VISIBLE
                    tvPlayer.visibility = View.VISIBLE
                }
            }

            override fun onAnimationEnd(animation: Animator) {
                if (!isReverse) {
                    tvOnline.visibility = View.GONE
                    translateView(tvPlayer, AppUtils.dp2px(54 + 14f) * 1.0f, true)
                    translateView(tvDealer, AppUtils.dp2px(54 + 14f) * 1.0f, false)
                }
            }

            override fun onAnimationCancel(animation: Animator) {}
            override fun onAnimationRepeat(animation: Animator) {}
        })
        animatorSet.start()
    }

    private fun translateView(
        view: View,
        distance: Float,
        toLeft: Boolean,
        isReverse: Boolean = false
    ) {
        val realDistance = if (toLeft) distance else -distance
        val start = if (isReverse) realDistance else 0f
        val end = if (isReverse) 0f else realDistance
        val translateAnimator =
            ObjectAnimator.ofFloat(view, "translationX", start, end)
        translateAnimator.duration = 350
        translateAnimator.start()
        translateAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {
            }

            override fun onAnimationEnd(animation: Animator) {
                if (isReverse) {
                    tvOnline.visibility = View.VISIBLE
                    tvDealer.visibility = View.GONE
                    tvPlayer.visibility = View.GONE
                    startScaleAnimation(true)
                }
            }

            override fun onAnimationCancel(p0: Animator) {}
            override fun onAnimationRepeat(p0: Animator) {}
        })
    }

    override fun onBackPressed() {
        if (tvDealer.visibility == View.VISIBLE) {
            translateView(tvDealer, AppUtils.dp2px(54 + 14f) * 1.0f, false, true)
            translateView(tvPlayer, AppUtils.dp2px(54 + 14f) * 1.0f, true, true)
        }
        //  再按一次退出
        if (System.currentTimeMillis() - currentTimeMillis > 2000) {
            currentTimeMillis = System.currentTimeMillis()
            AppUtils.show("再按一次退出")
            return
        }
        super.onBackPressed()
    }
}